'use strict';


app.controller('UsersController', ['$cookieStore','InfoUserService' , '$scope', '$http', '$state','$window','MODE', function($cookieStore,InfoUserService, $scope, $http, $state, $window, MODE) {
	$scope.$on("page.query", function(event, data) {
		InfoUserService.getCount($scope.queryParam).then(function(data) {
			if(data.code == '1001') {
				$scope.$broadcast("page.query.count", data);
			}
		});
		$scope.setUsersList();
	});
	
	$scope.setUsersList = function(){
		var page = $cookieStore.get("page");
		InfoUserService.getUserList(page, $scope.queryParam).then(function(data) {
			if(data.code == '1001') {
				console.log("获取用户资料信息:", data);
				$scope.userList = data.data;
				$scope.userList.sort(function(a, b) { //根据数组对象中的属性来排序 
					return -(parseInt(a.id) - parseInt(b.id));
				})
			} else if(data.code == '10010010') { //非法请求
				$state.go("app.users");
			};
		});
	};
	
	$scope.formData = {
		"sex": "男"
	};
	
	$scope.query = function() {
		$scope.setUsersList($scope.page.pageNo);
	}
	
	
	
	
	
	$scope.allotUserRole = function(userId){
		$scope.userRole1 = true;
		$scope.allotRole = false;
		$scope.userId = userId;
		$scope.$broadcast("setUserRoleList",userId);
		$scope.userList.forEach(function(user) {
			if(user.userId == userId) {
				$scope.userRole = user;
			}
		});
	};
	
	$scope.setAllotRole = function(roleId){
		$scope.allotRole = true;
		$scope.userRole1 = false;
		$scope.$broadcast("setUserRole",roleId);
	};
	
	$scope.setAddUser = function(){
		$scope.formData = {
			"sex": "男"
		};
		$scope.addUsers = true;
		$scope.mode = MODE.add;
	};
	
	$scope.edit = function(id){
		$scope.addUsers = true;
		$scope.mode = MODE.edit;
		$scope.userList.forEach(function(user) {
			if(user.id == id) {
				$scope.formData = user;
			}
		});
	};
	
	$scope.remove = function(id) {
		if($window.confirm("确定要删除该条信息?")) {
			InfoUserService.deleteById(id).then(function(data) {
				if(data.code == '1001') {
					$scope.$emit("page.query");
					$scope.setUsersList($scope.page.pageNo);
				} else {
					alert('删除失败');
				};

			});
		}
	}
	
}]);
app.controller('infoUserRoleCtl',function($cookieStore, $state, $scope, $window, InfoUserService, infoRoleService, InfoUserRoleService, MODE){
	
	$scope.$on("setUserRole",function(event,roleId){
		$scope.roleList.forEach(function(role){
			if(role.id == roleId){
				$scope.userRole.roleId = role.id;
			}
		})
	});
	
	
	$scope.$on("setUserRoleList", function(event, userId) {
		$scope.setUserRoleList(userId);
	});
	
	$scope.setUserRoleList = function(userId) {
		var page = $cookieStore.get("pages");
		infoRoleService.getRoleList(page, $scope.queryParam).then(function(data) {
			if(data.code == '1001') {
				$scope.roleList = data.data;
				$scope.userRoleList = {};
				InfoUserRoleService.getUserRole(userId).then(function(data) {
					$scope.userRoleList = data.data;
					$scope.roleList.forEach(function(role) {
						role.exit = false;
						$scope.userRoleList.forEach(function(userRole) {
							if(role.id == userRole.roleId) {
								role.exit = true;
								role.userRoleId = userRole.id
							}
						});
					})
				});

			} else if(data.code == '10010010') { //非法请求
				$state.go("app.users");
			};
		});
	};
	$scope.$on("page.querys", function(event, data) {
		infoRoleService.getCount($scope.queryParam).then(function(data){
			if (data.code=='1001') {
				console.log("data",data);
				$scope.$broadcast("page.query.counts",data);
				
			}
		});
		$scope.setUserRoleList($scope.userId);
	});
	$scope.addUserRole = function() {
		var userRole = $scope.userRole;
		InfoUserRoleService.addUserRole(userRole).then(function(data) {
			if(data.code == '1001') {
				$scope.userRole.desc = "";
				$scope.$emit("page.querys");
				$scope.allotUserRole(userRole.userId);
			} else {
				alert("插入失败");
			};
		});

	}

	$scope.removeUserRole = function(userRoleId) {
		if($window.confirm("确定要取消用户角色?")) {
			InfoUserRoleService.deleteById(userRoleId).then(function(data) {
				if(data.code == '1001') {
					$scope.$emit("page.querys");
					$scope.allotUserRole($scope.userId);
				} else {
					alert('删除失败');
				};

			});
		}
	}
});

app.controller('modalAdd', ['$cookieStore','InfoUserService' , '$scope', '$http', '$state','$window','MODE', function($cookieStore,InfoUserService, $scope, $http, $state, $window, MODE) {
	$scope.submit = function() {
		var user = $scope.formData;
		console.log("form",$scope.formData);
		if($scope.mode == MODE.add) {
			InfoUserService.addUser(user).then(function(data) {
				console.log("qwe",data);
				if(data.code == '1001') {
				
					$scope.$emit("page.query");
				} else {
					alert("插入失败");
				};
			});
		} else if($scope.mode == MODE.edit) {
			InfoUserService.updateUser(user).then(function(data) {
				if(data.code == '1001') {
					$scope.setUsersList($scope.page.pageNo);
				} else {
					alert("更新失败");
				};
			});
		}
		
		$('#addUserModal').modal('hide');
		$('#addUserModal').on('hidden.bs.modal', function() {
			$scope.close();
			
		})
	}


	$scope.close = function() {
		
		console.log("formData:",$scope.formData);
		$scope.userform.$setPristine();
		$scope.userform.$setUntouched();
		console.log("formData:",$scope.formData);
		
	}
}]);